<?php

namespace app\site\controller\store;

use app\common\exception\MiException;
use app\common\service\CommonOrderRefundService;
use app\common\service\pay\WxpayService;
use app\common\service\CommonSiteConfigService;
use app\model\site\SiteAddress;
use app\model\store\Order;
use app\model\store\OrderRefund;
use app\Request;
use app\site\controller\AController;
use app\site\service\site\OrderRefundService;
use app\site\service\site\OrderService;
use think\facade\Db;
use think\response\Json;

class OrderRefundController extends AController
{

    public function index(){

        $where = [];
        $key = $this->param('where.key');
        if ($key){
            $orderIds = Order::where('id|order_no', $key)->column('id');
            $where[] = ['order_id', 'in', $orderIds];
        }
        return $this->reqSer->setService(OrderRefundService::instance()->setWhere($where))->setPaginate()->getResponse();
    }

    /**
     * @throws MiException
     */
    public function edit(Request $request){
        if ($request->isPost()){
            return $this->reqSer->setService(OrderRefundService::instance())->edit()->getResponse();
        }
    }


    // 退款单审核
    /**
     * @throws MiException
     */
    public function audit(Request $request): Json
    {
        checkSiteAuth('store.orderRefund/edit');

        if ($request->isPost()){
            Db::transaction(function () {
                $id = $this->post('id');
                $status = $this->post('audit_status');
                if (!$id){
                    throw new MiException(PARAM_ERROR);
                }
                // 使用加锁查询
                $orderRefund = OrderRefund::where('id', $id)->lock(true)->find();
                $refundType = $orderRefund->refund_type;
                if ($orderRefund->audit_status != 0){   // 如果不是未审核
                    throw new MiException("请勿重复审核");
                }
                if ($status == 1){  // 同意退款
                    $orderRefund->audit_status = 1;

                    if ($refundType == 1){ // 仅退款
                        // 直接退款
                        CommonOrderRefundService::instance()->refundAction($orderRefund);
                    }elseif ($refundType == 2){ // 退款退货
                        // 添加退货地址
                        $address = $this->post('refundAddress');
                        if (!$address) throw new MiException("缺少退货地址");
                        $orderRefund->refund_address = serialize($address);
                    }

                }else{  // 不同意退款
                    $note = $this->post('refund_note');
                    if (!$note){
                        throw new MiException("没有填写拒绝原因");
                    }
                    $orderRefund->refund_note = $note;
                    $orderRefund->audit_status = -1;
                }
                // 保存退款信息
                $orderRefund->save();

            });

            return jsonSuccess();

        }else{
            throw new MiException(REQUEST_ERROR);
        }
    }

    // 退款
    public function refund(){
        Db::transaction(function () {
            $id = $this->post('id');
            $info = OrderRefund::lock(true)->find($id);
            if (!$info) throw new MiException("订单不存在");
            // 退款
            CommonOrderRefundService::instance()->refundAction($info);
        });

        return jsonSuccess();

    }

}
